package com.jhh.pay.heli.utils;

import com.jhh.pay.heli.bean.withhold.SingleWithholdResult;
import com.jhh.pay.heli.constant.Constant;
import com.jhh.pay.heli.bean.pay.SinglePayForPersonResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zhangqi on 2017/11/29.
 */
public class Util {

    private static final Logger mLogger = LoggerFactory.getLogger(Util.class);

    public static String RSAsign(LinkedHashMap<String,String> sign, String privateKey) throws Exception {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> signKey : sign.entrySet()) {
            sb.append(Constant.SPLIT).append(signKey.getValue());
        }
        String signs = RSA.sign(sb.toString(), RSA.getPrivateKey(privateKey));
        mLogger.info("签名前的key: "+sb.toString());
        mLogger.info("签名后的sign: "+signs);
        return signs;
    }

    public static String MD5sign(LinkedHashMap<String,String> sign, String md5sign) throws Exception {
        StringBuffer sb = new StringBuffer();
        for (Map.Entry<String, String> signKey : sign.entrySet()) {
            sb.append(Constant.SPLIT).append(signKey.getValue());
        }
        sb.append(Constant.SPLIT).append(md5sign);
        String signs = Disguiser.disguiseMD5(sb.toString());
        mLogger.info("签名前的key: "+sb.toString());
        mLogger.info("签名后的sign: "+signs);
        return signs;
    }

    /**
     * 代付结果验签
     * @param body
     * @param type  代付 和代付查询
     * @return
     */
    public static String PaytoMD5(SinglePayForPersonResult body,String type,String md5Key) {
        StringBuffer sb = new StringBuffer();
        sb.append(Constant.SPLIT)
                .append(body.getRt1_bizType())
                .append(Constant.SPLIT)
                .append(body.getRt2_retCode())
                .append(Constant.SPLIT)
                .append(body.getRt4_customerNumber())
                .append(Constant.SPLIT)
                .append(body.getRt5_orderId())
                .append(Constant.SPLIT)
                .append(body.getRt6_serialNumber());

        if(Constant.MD5_TYPE_QUERY.equals(type)){
            sb.append(Constant.SPLIT)
                    .append(body.getRt7_orderStatus())
                    .append(Constant.SPLIT)
                    .append(md5Key);
        }else if(Constant.MD5_TYPE_PAY.equals(type)){
            sb.append(Constant.SPLIT)
                    .append(md5Key);
        }

        mLogger.info("签名前的key: "+sb.toString());
        return Disguiser.disguiseMD5(sb.toString());
    }

    /**
     * 代扣结果验签
     * @param body
     * @return
     */
    public static String WithholdtoMD5(SingleWithholdResult body,String type,String md5Key) {
        StringBuffer sb = new StringBuffer();
        sb.append(Constant.SPLIT)
                .append(body.getRt1_bizType())
                .append(Constant.SPLIT)
                .append(body.getRt2_retCode())
                .append(Constant.SPLIT)
                .append(body.getRt3_retMsg())
                .append(Constant.SPLIT)
                .append(body.getRt4_customerNumber())
                .append(Constant.SPLIT)
                .append(body.getRt5_orderId())
                .append(Constant.SPLIT)
                .append(body.getRt6_serialNumber())
                .append(Constant.SPLIT)
                .append(body.getRt7_orderAmount())
                .append(Constant.SPLIT)
                .append(body.getRt8_bankName())
                .append(Constant.SPLIT)
                .append(body.getRt9_bankCode())
                .append(Constant.SPLIT)
                .append(body.getRt10_onlineCardType())
                .append(Constant.SPLIT)
                .append(body.getRt11_completeDate())
                .append(Constant.SPLIT)
                .append(body.getRt12_cardAfterFour())
                .append(Constant.SPLIT)
                .append(body.getRt13_orderStatus());
        if(Constant.MD5_TYPE_QUERY_WITHHOLD.equals(type)){
            sb.append(Constant.SPLIT)
                    .append(md5Key);
        }else if(Constant.MD5_TYPE_WITHHOLD.equals(type)){
            sb.append(Constant.SPLIT)
                    .append(md5Key);
        }else if(Constant.MD5_TYPE_CALLBACK_WITHHOLD.equals(type)){
            sb.append(Constant.SPLIT)
                    .append(md5Key);
        }
        mLogger.info("签名前的key: "+sb.toString());
        return Disguiser.disguiseMD5(sb.toString());
    }

}
